<?php
/**
 * 
 * +------------------------------------------------------------+
 * @category E 
 * +------------------------------------------------------------+
 * 框架内置异常处理类
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.judianyun.com 2013
 * @version 1.0
 *
 * Modified at : 2013-2-16 17:29:44
 *
 */

define('EXCEPTION_T_SYS',	0);
define('EXCEPTION_T_DB',	1);
define('EXCEPTION_T_APP',	2);
define('EXCEPTION_T_ERR',	3);

class E extends Exception{
	
	private $_t;
	
	private $_method;
	
	private $_reflection = array();
	
	function __construct($message, $t=EXCEPTION_T_SYS, $code=5, $method = '', $reflection = array()){
		parent::__construct($message, $code);
		$this->_t = $t;
		$this->_method = $method;
		$this->_reflection = $reflection;
	}
	
	
	/**
	 * 
	 * +------------------------------------------------------------+
	 * @name halt
	 * +------------------------------------------------------------+
	 * 输出或保存错误日志
	 * +------------------------------------------------------------+
	 *
	 * @author anzai <236049524@qq.com>
	 * @version 1.0
	 *
	 * @example
	 *
	 * @param mixed $message 错误信息
	 * @param string $type   信息类型
	 * @param boolean $force 是否强制输出，设置true时，任何运行模式都会输出错误提示信息
	 *
	 */
	static function halt($message, $type=EXCEPTION_T_SYS, $force=false){
		$title = self::_get_title($type);
		$msg = is_array($msg) ? implode('</pre><pre>', $message) : $message;
		
		if (RUNNING_MODE == 1 || $force){
			require __EXCEPTION_PATH__ . 'debug.html';
		}else{
			Helper::send_status('404');
			require __EXCEPTION_PATH__ . '404.html';
			$logfile = TEMP_PATH . '/log/debug_' . date('Ymd') . '.log';
			Helper::write($logfile, '[' . $title . '][' . date('H:i:s') . ']' . (is_array($msg) ? implode(',', $message) : $message) . "\n", "a+");
		}
		exit;
	}
	
	//获取错误信息
	public function getMsg(){
		ob_start();
		$title = $this->_get_title($this->_type);
		$msg = $this->message;
		$file = $this->file;
		$trace = $this->getTrace();
		$class = $trace[0]['class'];
		$method = $trace[0]['function'];
		$line = $this->line;
		if (RUNNING_MODE){
			require_once __EXCEPTION_PATH__ . 'log.html';
		}else{//正式上线模式保存异常信息
			$log = Helper::L('CLS_E_INFO') . $msg . "\n";
			$log .= Helper::L('CLS_E_FILE') . $file . "\n";
			if($class)$log .= Helper::L('CLS_E_CLASS') . $class . "\n";
			if($method)$log .= Helper::L('CLS_E_METHOD') . $method . "\n";
			$log .= Helper::L('CLS_E_LINE') . $line . "\n";
			if ($this->_method != ''){
				$log .= '执行函数：' . $this->_method . "\n";
			}
			
			if ($this->_reflection){
				$log .= '来自：' . $this->_reflection['from'] . "\n";
				$log .= '源文件：' . $this->_reflection['file'] . "\n";
			}
			
			$log .= "/--------------/\n";
			Helper::write(TEMP_PATH . '/log/exp_' . date('Ymd') . '.log', $log, 'a+');
		}
		$out = ob_get_clean();
		exit($out);
	}
	
	///根据错误信息类型获取标题
	static private function _get_title($type){
		switch (strtolower($type)){
			case EXCEPTION_T_DB:
				$title = Helper::L('CLS_E_DB_ERR');
				break;
			case EXCEPTION_T_SYS:
				$title = Helper::L('CLS_E_SYS_ERR');
				break;
			case EXCEPTION_T_APP:
				$title = Helper::L('CLS_E_APP_ERR');
				break;
			default:
				$title = Helper::L('CLS_E_OTHER_ERR');
		}
		return $title;
	}
	
	///将对象转换为错误信息
	function __toString(){
		return $this->getMsg();
	}
}

?>